<?php
/**
 * This functions collection will be used instead of multiple copies of alot of code everywhere, which
 * makes it increadibly much more easy to maintain.
 */

include_once("administrator/components/com_virtuemart/classes/payment/ps_kreditor.cfg.php");

/**
 * This method returns the inoive fee of a specific country. If no country is selected given
 * it will automatically get the invoice fee of the country from where the user is.
 * 
 * This method is unfortunately not affected by the currency converter yet. Which means if the
 * currency is changed during the last step, this will not change. However, there shall! be a
 * safety check at the end of each of Klarnas payments to make sure that currency differations
 * wont work. A swedish customer must buy in swedish kronor.
 * 
 * Created: v2.35
 * Last modified: v2.35 DO NOT FORGET TO UPDATE WHEN CHANGING
 * 
 * @param $countrie optional, if you want to specify a country from which to get the invoice cost.
 * @return int/double invoice fee of a specific country.
 */
function getKlarnaInvoiceCost($countrie = '') {
	if($countrie == '') {
		$db = new ps_DB;
		$q  = "SELECT * from #__{vm}_user_info WHERE user_id='" . $_SESSION['auth']['user_id'] . "'";
		$db->query($q);
		$countrie = $db->f('country');
	}
	
	switch($countrie) {
		case "SWE": return KLARNA_INVOICE_FEE_SWE;
		case "NOR": return KLARNA_INVOICE_FEE_NOR;
		case "DNK": return KLARNA_INVOICE_FEE_DNK;
		case "DEU": return KLARNA_INVOICE_FEE_DEU;
		case "FIN": return KLARNA_INVOICE_FEE_FIN;
		case "NLD": return KLARNA_INVOICE_FEE_NLD;
	}
}

/**
 * This function works to replace the old KREDITOR_VAT_INVOICE_FEE and to make it a 1 place
 * seeder of the invoice tax level for all countries.
 * 
 * Created: v2.35
 * Last modified: v2.35 DO NOT FORGET TO UPDATE WHEN CHANGING
 * 
 * @param $cart this is used by account.order_details.tpl.php but its an modifyed array with only
 * product id's. <b>DEFAULTS to $_SESSION['cart']</b>
 * 
 * @return returns a number with format: 5.5 or 25 or 2 NOT 0.055/0.25/0.02 etc.
 */
function getKlarnaInvoiceTax($cart = '') {
	$db2 = new ps_DB;
	$q2 = "SELECT vendor_country FROM #__{vm}_vendor WHERE vendor_id = '" . $_SESSION['ps_vendor_id'] . "';";
	$db2->query($q2);
	$vendor_countrie = $db2->f("vendor_country");
	
	switch($vendor_countrie) {
		case "SWE": return getTheSwedishTaxAgencyTaxRate($cart);
		case "NOR": return '25';
		case "DNK": return '25';
		case "DEU": return '19';
		case "FIN": return '23';
		case "FIN": return '19';
	}
}

/**
 * Tries to mimic the calculation needed by law to get the correct tax rate.
 * For swedish customers only!
 * http://www.skatteverket.se/rattsinformation/skrivelser2005/02/skrivelser20020913.4.18e1b10334ebe8bc80001578.html
 * 
 * Created: v2.35
 * Last modified: v2.35 DO NOT FORGET TO UPDATE WHEN CHANGING
 * 
 * @param $cart this is used by account.order_details.tpl.php but its an modifyed array with only
 * product id's. <b>DEFAULTS to $_SESSION['cart']</b>
 * 
 * @return returns a number with format: 5.5 or 25 or 2 NOT 0.055/0.25/0.02 etc.
 */
function getTheSwedishTaxAgencyTaxRate($cart = '') {
	if(!$cart) {
		$cart = $_SESSION['cart'];
	}
	$withTax = 0;
	$withoutTax = 0;
	require_once(CLASSPATH ."ps_product.php");
	$ps_product = new ps_product();
	
	for ($i=0;$i<$cart["idx"];$i++)  {
		$prince_info = $ps_product->calcEndUserprice($cart[$i]["product_id"], '');
		if($_SESSION['auth']["show_price_including_tax"] == 1) {
			$withTax += $GLOBALS['CURRENCY']->convert( $prince_info["product_price"], @$prince_info["product_currency"] )*100;
			
			$_SESSION['auth']["show_price_including_tax"] = 0;
			$temp = $ps_product->calcEndUserprice($cart[$i]["product_id"], '');
			$temp = $GLOBALS['CURRENCY']->convert( $temp["product_price"], @$prince_info["product_currency"] )*100;
			$withoutTax += $temp;
			$_SESSION['auth']["show_price_including_tax"] = 1;
		} else {
			$withTax += ($GLOBALS['CURRENCY']->convert( $prince_info["product_price"], @$prince_info["product_currency"] )*100)*($ps_product->get_product_taxrate($cart[$i]["product_id"])+1);
			$withoutTax += $GLOBALS['CURRENCY']->convert( $prince_info["product_price"], @$prince_info["product_currency"] )*100;
		}
	}
	if($withTax != 0 && $withTax != 0) //Occures when refreshing the checkout.kreditor page.
	return round((($withTax/$withoutTax)-1), 5)*100;
}

/**
 * Used as a wrapper, to be easier to manipulate for future edeting.
 * 
 * Created: v2.35
 * Last modified: v2.35 DO NOT FORGET TO UPDATE WHEN CHANGING
 * 
 * @return getTheSwedishTaxAgencyTaxRate()
 */
function getSwedishShippingTaxRate() {
	return getTheSwedishTaxAgencyTaxRate();
}

?>